package com.crm.sys.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.im.core.constant.IMConstant;
import com.crm.im.core.handler.IMRequestHandler;
import com.crm.im.core.model.Message;
import com.crm.im.core.model.ReplyBody;
import com.crm.im.core.model.SentBody;
import com.crm.im.core.session.DefaultSessionManager;
import com.crm.im.core.session.IMSession;
import com.crm.sys.push.SystemMessagePusher;
 

/**
 * 退出连接实现
 */
@Service("logoutHandler")
public class LogoutHandler implements IMRequestHandler {
	@Autowired
	private DefaultSessionManager sessionManager; 
	@Autowired
	private SystemMessagePusher systemMessagePusher;
	
	public ReplyBody process(IMSession ios, SentBody message) { 
		String account =ios.getAttribute(IMConstant.SESSION_KEY).toString();
		ios.removeAttribute(IMConstant.SESSION_KEY);
		ios.closeNow();
		sessionManager.remove(account);
		 
		   if(!account.equals(1)){
			 //通知下线线消息
				List<IMSession>  sessionList = sessionManager.queryAll();
				for(IMSession s:sessionList){
					Message  msg = new Message();
					msg.setType(IMConstant.MessageType.TYPE_OFFLINE);//强行下线消息类型
					msg.setReceiver(s.getAccount());
					msg.setContent(account);
					systemMessagePusher.push(msg); 
				}
		   }
		
		return null;
	}
	
	
}